package com.wd.live.ui.dailog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView.Adapter
import com.wd.live.R
import com.wd.live.databinding.DialogSelectLiveTypeBinding
import com.wd.live.databinding.ItemTypeListBinding
import com.wd.live.tool.BindingViewHolder
import com.wd.live.tool.LocalData
import com.wd.live.tool.newBindingViewHolder
import com.wd.live.tool.viewBinding

/**
 * 选择直播类型
 * @author nick.wu
 */
class SelectLiveTypeDialog(context: Context, private val typeInfo: LocalData.TypeInfo?, private val function: (LocalData.TypeInfo) -> Unit) :
    Dialog(context, R.style.dialog_style) {

    private val binding: DialogSelectLiveTypeBinding by viewBinding()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        initDialogWindow(window, Gravity.BOTTOM)
        setCanceledOnTouchOutside(true)
        initView()
    }

    private fun initView() {
        binding.list.apply {
            val data = mutableListOf(
                LocalData.TypeInfo(101, "校园"),
                LocalData.TypeInfo(102, "美食"),
                LocalData.TypeInfo(103, "健身"),
                LocalData.TypeInfo(104, "跳舞"),
                LocalData.TypeInfo(104, "音乐")
            )
            //回显匹配逻辑
            data.forEach {
                if(it.id == typeInfo?.id) {
                    it.select = true
                    function.invoke(it)
                }
            }
            adapter = MyAdapter(
                data
            ) {
                function.invoke(it)
            }
            layoutManager = GridLayoutManager(context, 3)
        }
    }

    private fun initDialogWindow(window: Window?, gravity: Int) {
        if (window != null) {
            window.setGravity(gravity)
            window.decorView.setPadding(0, 0, 0, 0)
            //获得window窗口的属性
            val lp = window.attributes
            //设置窗口宽度为充满全屏
            lp.width = WindowManager.LayoutParams.MATCH_PARENT
            //设置窗口高度为包裹内容
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT
            //将设置好的属性set回去
            window.attributes = lp
        }
    }

    class MyAdapter(val data: MutableList<LocalData.TypeInfo>, private val function: (LocalData.TypeInfo) -> Unit) :
        Adapter<BindingViewHolder<ItemTypeListBinding>>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingViewHolder<ItemTypeListBinding> {
            return newBindingViewHolder(parent)
        }

        override fun getItemCount(): Int = data.size

        override fun onBindViewHolder(holder: BindingViewHolder<ItemTypeListBinding>, position: Int) {
            data[position].apply {
                holder.binding.type.text = name
                //选择状态
                holder.binding.type.isChecked = select
                holder.binding.type.setOnClickListener {
                    //执行点击回调
                    function.invoke(this)
                    holder.binding.type.isChecked = !holder.binding.type.isChecked
                    //先清空所有选中状态
                    data.forEach {
                        it.select = false
                    }
                    data[position].select = true
                    notifyDataSetChanged()
                }
            }
        }
    }
}